<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useWatermarkStore } from '../store/useWatermarkStore'
import { useAuthStore } from '../store/useAuthStore'
import { Upload, Picture, Download, Delete, Mic as Magic, Lock, Star, Check, Document, Lightning, Setting } from '@element-plus/icons-vue'
import type { UploadFile } from 'element-plus'
import { ProcessStatus } from '../types'

const router = useRouter()
const watermarkStore = useWatermarkStore()
const authStore = useAuthStore()
const processing = ref(false)
const showProTip = ref(false)

// 显示高级功能提示
const showProFeatures = () => {
  showProTip.value = true
}

// 关闭高级功能提示
const closeProTip = () => {
  showProTip.value = false
}

// 格式化文件大小
const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 处理文件选择
const handleFileSelected = (file: UploadFile) => {
  if (!file.raw) return
  
  // 检查文件类型
  if (!file.raw.type.startsWith('image/')) {
    ElMessage.error('请上传图片文件')
    return
  }
  
  // 检查文件大小（10MB）
  if (file.raw.size > 10 * 1024 * 1024) {
    ElMessage.error('文件大小不能超过10MB')
    return
  }
  
  watermarkStore.addFile({
    name: file.name,
    size: file.raw.size,
    type: file.raw.type,
    status: ProcessStatus.PENDING
  })
}

// 移除文件
const removeFile = (index: number) => {
  watermarkStore.removeFile(index.toString())
}

// 清空所有文件
const clearAllFiles = () => {
  watermarkStore.clearFiles()
}

// 处理文件
const processFiles = async () => {
  if (watermarkStore.files.length === 0) {
    ElMessage.warning('请先上传图片')
    return
  }
  
  processing.value = true
  try {
    for (const file of watermarkStore.files) {
      await watermarkStore.processFile(file.name)
    }
    ElMessage.success('处理完成')
  } catch (error) {
    ElMessage.error('处理失败，请重试')
  } finally {
    processing.value = false
  }
}

// 跳转到专业版
const goToPro = () => {
  router.push('/pricing')
}

// 文件列表
const fileList = ref<UploadFile[]>([])
</script>

<template>
  <div class="tools-page">
    <!-- 头部区域 -->
    <div class="header-section">
      <div class="header-content">
        <h1 class="animate-fade-up">专业图片水印工具</h1>
        <p class="animate-fade-up" style="--delay: 0.2s">快速、安全地为您的图片添加和删除水印，让您的图片更加专业</p>
      </div>
      <div class="header-wave">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320" preserveAspectRatio="none">
          <path fill="#ffffff" fill-opacity="1" d="M0,96L48,112C96,128,192,160,288,186.7C384,213,480,235,576,229.3C672,224,768,192,864,165.3C960,139,1056,117,1152,122.7C1248,128,1344,160,1392,176L1440,192L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path>
        </svg>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="container">
      <div class="tool-content">
        <!-- 上传区域 -->
        <div class="card upload-section animate-fade-up" style="--delay: 0.3s">
          <div class="upload-title-row">
            <div class="upload-icon">
              <el-icon><Upload /></el-icon>
            </div>
            <div class="upload-title-content">
              <h2>上传图片</h2>
              <p>支持 JPG、PNG 格式，单次最多可上传 10 张图片</p>
            </div>
          </div>
          <el-upload
            class="upload-area"
            drag
            action="#"
            :auto-upload="false"
            :on-change="handleFileSelected"
            :limit="10"
            accept="image/jpeg,image/png"
            :file-list="fileList"
          >
            <div class="img-container">
              <img 
                src="https://images.pexels.com/photos/7567434/pexels-photo-7567434.jpeg" 
                alt="上传图片" 
                class="upload-image"
              >
              <div class="img-overlay">
                <el-icon class="upload-plus-icon"><Upload /></el-icon>
                <span>拖拽或点击上传</span>
              </div>
            </div>
            <template #trigger>
              <el-button type="primary" class="btn-primary">
                <el-icon><Upload /></el-icon>
                选择图片
              </el-button>
            </template>
          </el-upload>
          <div class="action-buttons">
            <el-button 
              type="primary" 
              :disabled="watermarkStore.files.length === 0"
              :loading="processing"
              @click="processFiles"
              class="btn-primary"
            >
              <el-icon><Magic /></el-icon>
              开始处理
            </el-button>
            <el-button 
              v-if="watermarkStore.files.length > 0"
              type="info" 
              plain
              @click="clearAllFiles"
              class="btn-secondary"
            >
              清空列表
            </el-button>
          </div>
        </div>

        <!-- 文件列表 -->
        <div v-if="watermarkStore.files.length > 0" class="card files-section animate-fade-up" style="--delay: 0.4s">
          <div class="card-header">
            <h2>待处理文件</h2>
            <el-button type="primary" @click="goToPro" class="btn-primary btn-sm">升级到专业版</el-button>
          </div>
          <div class="files-table">
            <el-table :data="watermarkStore.files" style="width: 100%">
              <el-table-column prop="name" label="文件名" min-width="200">
                <template #default="{ row }">
                  <div class="file-info">
                    <el-icon><Document /></el-icon>
                    <span class="file-name">{{ row.name }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="size" label="大小" width="120">
                <template #default="{ row }">
                  <span class="file-size">{{ formatFileSize(row.size) }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="status" label="状态" width="120">
                <template #default="{ row }">
                  <span class="file-status" :class="row.status">
                    {{ row.status === 'pending' ? '待处理' : 
                       row.status === 'processing' ? '处理中' : 
                       row.status === 'completed' ? '已完成' : 
                       row.status === 'error' ? '错误' : '未知' }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="120" fixed="right">
                <template #default="{ $index }">
                  <el-button 
                    type="danger" 
                    link
                    @click="removeFile($index)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>

        <!-- 空状态 -->
        <div v-else class="card empty-state animate-fade-up" style="--delay: 0.4s">
          <div class="empty-icon">
            <el-icon><Picture /></el-icon>
          </div>
          <h3>暂无待处理文件</h3>
          <p>请上传需要添加水印的图片</p>
        </div>

        <!-- 信息区域 -->
        <div class="info-section">
          <!-- 功能特点 -->
          <div class="feature-card">
            <div class="card-title">功能特点</div>
            <div class="feature-list">
              <div class="feature-item">
                <div class="icon-box">
                  <el-icon><Lock /></el-icon>
                </div>
                <div class="feature-text">
                  <div class="feature-name">安全可靠</div>
                  <div class="feature-desc">所有处理都在本地完成，不会上传到服务器，保障您的隐私安全</div>
                </div>
              </div>
              <div class="feature-item">
                <div class="icon-box">
                  <el-icon><Lightning /></el-icon>
                </div>
                <div class="feature-text">
                  <div class="feature-name">快速处理</div>
                  <div class="feature-desc">采用高效算法，支持批量处理，大幅提高您的工作效率</div>
                </div>
              </div>
              <div class="feature-item">
                <div class="icon-box">
                  <el-icon><Setting /></el-icon>
                </div>
                <div class="feature-text">
                  <div class="feature-name">灵活配置</div>
                  <div class="feature-desc">可自定义水印样式、位置和透明度，满足各种使用场景需求</div>
                </div>
              </div>
            </div>
          </div>

          <!-- 使用指南 -->
          <div class="guide-card">
            <div class="card-title">使用指南</div>
            <div class="guide-list">
              <div class="guide-item">
                <div class="step-num">1</div>
                <div class="step-content">
                  <div class="step-title">上传图片</div>
                  <div class="step-desc">选择需要添加水印的图片文件，支持JPG和PNG格式</div>
                </div>
              </div>
              <div class="guide-item">
                <div class="step-num">2</div>
                <div class="step-content">
                  <div class="step-title">设置水印</div>
                  <div class="step-desc">选择水印样式和位置，可自定义文字或图片水印</div>
                </div>
              </div>
              <div class="guide-item">
                <div class="step-num">3</div>
                <div class="step-content">
                  <div class="step-title">开始处理</div>
                  <div class="step-desc">点击处理按钮，系统会自动为图片添加水印</div>
                </div>
              </div>
              <div class="guide-item">
                <div class="step-num">4</div>
                <div class="step-content">
                  <div class="step-title">下载结果</div>
                  <div class="step-desc">处理完成后下载带水印的图片，高质量保存</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Pro功能提示 -->
    <el-dialog
      v-model="showProTip"
      title="升级到专业版"
      width="700px"
      class="pro-dialog"
      destroy-on-close
    >
      <div class="pro-content">
        <div class="pro-icon">
          <el-icon><Star /></el-icon>
        </div>
        <h3>解锁更多专业功能</h3>
        <ul class="pro-features">
          <li>
            <el-icon><Check /></el-icon>
            <span>批量处理更多图片</span>
          </li>
          <li>
            <el-icon><Check /></el-icon>
            <span>支持更大文件尺寸</span>
          </li>
          <li>
            <el-icon><Check /></el-icon>
            <span>高级水印去除算法</span>
          </li>
          <li>
            <el-icon><Check /></el-icon>
            <span>优先处理队列</span>
          </li>
          <li>
            <el-icon><Check /></el-icon>
            <span>自定义水印模板</span>
          </li>
          <li>
            <el-icon><Check /></el-icon>
            <span>批量导出多种格式</span>
          </li>
        </ul>
        <div class="dialog-buttons">
          <el-button @click="closeProTip" class="btn-secondary">暂不升级</el-button>
          <el-button type="primary" @click="goToPro" class="btn-primary">立即升级</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
/* 全新布局样式 */
.tools-page {
  min-height: 100vh;
  height: auto;
  background-color: var(--bg-color);
}

/* 新的头部区域 */
.header-section {
  background: linear-gradient(135deg, #36cfc9 0%, #13c2c2 100%);
  color: white;
  padding: 80px 24px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.header-content {
  max-width: 700px;
  margin: 0 auto;
  position: relative;
  z-index: 2;
}

.header-content h1 {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 16px;
  text-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.header-content p {
  font-size: 1.1rem;
  margin-bottom: 24px;
  opacity: 0.9;
}

.header-wave {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  overflow: hidden;
}

/* 主内容区域 - 单列布局 */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  overflow: visible;
}

.tool-content {
  display: flex;
  flex-direction: column;
  margin-top: -50px;
  position: relative;
  z-index: 2;
}

/* 功能区卡片样式 */
.card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  height: 100%; /* 确保卡片高度一致 */
  margin-bottom: 32px;
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 30px rgba(0,0,0,0.12);
}

/* 上传区域 */
.upload-section {
  padding: 40px;
  text-align: center;
}

.upload-title-row {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 24px;
}

.upload-icon {
  width: 60px;
  height: 60px;
  background: var(--primary-gradient);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  color: white;
  font-size: 24px;
  box-shadow: 0 4px 12px rgba(19, 194, 194, 0.25);
}

.upload-title-content h2 {
  font-size: 1.5rem;
  margin: 0 0 4px;
  text-align: left;
}

.upload-title-content p {
  color: var(--text-secondary);
  margin: 0;
  text-align: left;
}

.upload-area {
  max-width: 800px;
  margin: 32px auto;
}

.img-container {
  position: relative;
  width: 100%;
  height: 240px;
  border-radius: 8px;
  overflow: hidden;
  background-color: #f5f7fa;
  border: 2px dashed #dcdfe6;
  transition: all 0.3s;
}

.img-container:hover {
  border-color: var(--primary-color);
}

.upload-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.img-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  opacity: 0;
  transition: opacity 0.3s;
}

.img-container:hover .img-overlay {
  opacity: 1;
}

.img-container:hover .upload-image {
  transform: scale(1.05);
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 24px;
}

.btn-primary {
  background: var(--primary-gradient);
  border: none;
  box-shadow: 0 4px 12px rgba(19, 194, 194, 0.25);
  padding: 12px 28px;
  font-size: 1rem;
  transition: all 0.3s;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(19, 194, 194, 0.35);
}

.btn-secondary {
  border: 1px solid var(--primary-color);
  color: var(--primary-color);
  padding: 12px 28px;
  font-size: 1rem;
}

/* 文件列表区域 */
.files-section {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 32px;
  border-bottom: 1px solid var(--border-color);
  background-color: #f9fafb;
}

.files-table {
  padding: 16px 32px 32px;
}

.file-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.file-name {
  font-weight: 500;
  word-break: break-word;
}

.file-status {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 0.875rem;
  font-weight: 500;
}

.file-status.pending {
  background-color: #e6f7ff;
  color: #1890ff;
}

.file-status.processing {
  background-color: #fff7e6;
  color: #fa8c16;
}

.file-status.completed {
  background-color: #f6ffed;
  color: #52c41a;
}

.file-status.error {
  background-color: #fff1f0;
  color: #f5222d;
}

/* 空状态样式 */
.empty-state {
  padding: 48px 24px;
  text-align: center;
}

.empty-icon {
  width: 80px;
  height: 80px;
  background-color: rgba(19, 194, 194, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 24px;
  color: var(--primary-color);
  font-size: 32px;
}

.empty-state h3 {
  font-size: 1.25rem;
  margin-bottom: 8px;
}

.empty-state p {
  color: var(--text-secondary);
}

/* 信息区域样式优化 - 修复文字对齐问题 */
.info-section {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  margin: 30px 0;
}

.feature-card, .guide-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  padding: 30px;
}

/* 标题改为左对齐，带左侧色块 */
.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  text-align: left;
  margin-bottom: 30px;
  position: relative;
  padding-left: 15px;
  border-left: 4px solid var(--primary-color);
}

/* 移除原来的下划线 */
.card-title:after {
  content: none;
}

/* 功能特点样式 - 改进垂直对齐 */
.feature-list {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.feature-item {
  display: flex;
  align-items: flex-start; /* 改为顶部对齐 */
}

.icon-box {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  margin-top: 2px; /* 微调图标位置，与文字第一行对齐 */
  flex-shrink: 0;
  box-shadow: 0 3px 8px rgba(19, 194, 194, 0.2);
}

.icon-box .el-icon {
  font-size: 20px;
  color: white;
}

.feature-text {
  flex: 1;
  text-align: left;
}

.feature-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.2;
}

.feature-desc {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

/* 使用指南样式 - 改进垂直对齐 */
.guide-list {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.guide-item {
  display: flex;
  align-items: flex-start; /* 改为顶部对齐 */
}

.step-num {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary-gradient);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  margin-right: 16px;
  margin-top: 2px; /* 微调图标位置，与文字第一行对齐 */
  flex-shrink: 0;
  box-shadow: 0 3px 8px rgba(19, 194, 194, 0.2);
}

.step-content {
  flex: 1;
  text-align: left;
}

.step-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.2;
}

.step-desc {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

/* Pro对话框样式 */
.pro-dialog :deep(.el-dialog__header) {
  padding: 24px;
  background: var(--primary-gradient);
  color: white;
}

.pro-dialog :deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
}

.pro-dialog :deep(.el-dialog) {
  border-radius: 12px;
  overflow: hidden;
}

.pro-dialog :deep(.el-dialog__body) {
  padding: 0;
}

.pro-content {
  padding: 32px;
}

.pro-icon {
  width: 72px;
  height: 72px;
  background: var(--primary-gradient);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 24px;
  color: white;
  font-size: 28px;
  box-shadow: 0 4px 12px rgba(19, 194, 194, 0.25);
}

.pro-content h3 {
  font-size: 1.5rem;
  margin-bottom: 24px;
}

.pro-features {
  list-style: none;
  padding: 0;
  margin: 32px 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.pro-features li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background-color: #f9fafb;
  border-radius: 8px;
  transition: all 0.3s;
}

.pro-features li:hover {
  background-color: rgba(19, 194, 194, 0.05);
  transform: translateY(-3px);
}

.pro-features li .el-icon {
  color: var(--primary-color);
  font-size: 18px;
}

.dialog-buttons {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 32px;
}

/* 临时禁用动画，使截屏工具能正常工作 */
.animate-fade-up {
  opacity: 1;
  transform: none;
  animation: none;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .header-section {
    padding: 60px 24px;
  }
  
  .header-content h1 {
    font-size: 2rem;
  }
  
  .upload-section {
    padding: 24px;
  }
  
  .upload-title-row {
    flex-direction: column;
    text-align: center;
  }
  
  .upload-icon {
    margin-right: 0;
    margin-bottom: 16px;
  }
  
  .upload-title-content h2, 
  .upload-title-content p {
    text-align: center;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .btn-primary, .btn-secondary {
    width: 100%;
  }
  
  .card-header {
    flex-direction: column;
    gap: 16px;
  }
  
  .card-header .btn-sm {
    width: 100%;
  }
  
  .pro-features {
    grid-template-columns: 1fr;
  }
  
  .info-section {
    grid-template-columns: 1fr;
  }
  
  .card-title {
    text-align: left;
  }
  
  .card-title:after {
    left: 0;
    transform: none;
  }
}

@media (max-width: 576px) {
  .header-content h1 {
    font-size: 1.8rem;
  }
  
  .files-section, .feature-card, .guide-card {
    padding: 16px;
  }
  
  .img-container {
    height: 180px;
  }
  
  .upload-plus-icon {
    font-size: 24px;
  }
  
  .feature-item, .guide-item {
    flex-direction: row; /* 修改为行方向，保持图标在左侧 */
    align-items: flex-start;
  }
  
  .icon-box, .step-num {
    margin-bottom: 0; /* 移除底部间距 */
    margin-right: 16px; /* 保持右侧间距 */
  }
  
  .guide-item:not(:last-child)::after {
    left: 24px;
    top: 60px;
    height: calc(100% - 48px);
  }
  
  .dialog-buttons {
    flex-direction: column;
  }
  
  .dialog-buttons .el-button {
    width: 100%;
  }
}
</style> 